<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/8/1
 * Time: 1:04 PM
 */
namespace app\common\services\theme\datas;

use app\common\models\CarouselItem;

class Carousel
{
    /**
     * @param $id
     * @param $isEdit是否是后端编辑，后端编辑和前端显示不一样
     * @return array
     */
    public function getData($id,$isEdit=false){
        if(!$id){
            return [];
        }
        if($isEdit){
            //后端编辑显示导航数据
            $list = \app\common\models\Carousel::select(['id','name'])->where('id',$id)->first();
            $list = [$list];
        }else{
            $query = CarouselItem::site()->select();
            $list = $query
                ->where('carousel_id',$id)
                ->orderBy('display_order', 'desc')
                ->orderBy('id', 'desc')
                ->get();
            if(!$list){
                return [];
            }
            $list->map(function ($v){
                $v->thumb = fmt_media_url($v->thumb);
            });
        }

        return $list;
    }
}
